package com.service.impl;

import com.annotaion.Lock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class TimeTaskServiceImpl {

    @Value("${server.port}")
    private  String port;

    @Autowired
    private RedissonClient redissonClient;

    @Scheduled(cron = "0/10 * * * * ?")
    public void sendMessage() throws InterruptedException {


        RLock rLock=redissonClient.getLock("hello");
        boolean f=rLock.tryLock(3000,5000, TimeUnit.MILLISECONDS);
        if (f){
            System.out.println("我获取锁了，准备发送信息");
            Thread.sleep(4000);

        }else{
            System.out.println("我没有获取锁就要退出了");

        }


    }

    @Scheduled(cron = "0/10 * * * * ?")
    @Lock(name="happy",waitTime=3000,leaveTime=5000)
    public void sendMessage2() throws InterruptedException{
        System.out.println(port+"发来Happy birthday:"+new Date().toLocaleString());
    }



}
